  <?php
  $ip      = $_SERVER['REMOTE_ADDR']; // Mendapatkan IP komputer user
  $tanggal = date("Ymd"); // Mendapatkan tanggal sekarang
  $waktu   = time(); // 

  // Mencek berdasarkan IPnya, apakah user sudah pernah mengakses hari ini 
  $query = Counter::model()->findAll("ip='".$ip."' and tanggal='".$tanggal."'");
    // Kalau belum ada, simpan data user tersebut ke database
  
  if(count($query) == 0){
     $qry = "INSERT INTO counter(ip, tanggal, hits, online) VALUES('$ip','$tanggal','1','$waktu')";
     $cmd = Yii::app()->db->createCommand($qry)->execute();
  } 
  else{
    $qry = "UPDATE counter SET hits=hits+1, online='$waktu' WHERE ip='$ip' AND tanggal='$tanggal'";
    $cmd = Yii::app()->db->createCommand($qry)->execute();
  }


  $pengunjung = count(Counter::model()->findAll("tanggal='".$tanggal."' GROUP BY ip"));
  //$pengunjung       = mysql_num_rows(mysql_query("SELECT * FROM counter WHERE tanggal='$tanggal' GROUP BY ip"));
  //$qryTotal = 'SELECT COUNT(hits) FROM counter';
  //$totalpengunjung = Yii::app()->db->createCommand($qryTotal)->queryAll();
  //$totalpengunjung  = mysql_result(mysql_query("SELECT COUNT(hits) FROM counter"), 0); 
  //$hits             = mysql_fetch_assoc(mysql_query("SELECT SUM(hits) as hitstoday FROM counter WHERE tanggal='$tanggal' GROUP BY tanggal")); 
  //$totalhits        = mysql_result(mysql_query("SELECT SUM(hits) FROM counter"), 0); 
  //$tothitsgbr       = mysql_result(mysql_query("SELECT SUM(hits) FROM counter"), 0); 
  $bataswaktu       = time() - 100;
  $pengunjungonline = count(Counter::model()->findAll("online>'".$bataswaktu."'"))
  //$pengunjungonline = mysql_num_rows(mysql_query("SELECT * FROM counter WHERE online > '$bataswaktu'"));

  /*
  $tothitsgbr = sprintf("%06d", $tothitsgbr);
  for ( $i = 0; $i <= 9; $i++ ){
	   $tothitsgbr = str_replace($i, "<img src='$path$i$ext' alt='$i'>", $tothitsgbr);
  }
  */
  ?>

  <div class="col-md-3" style="min-height:350px;border-right:1px dotted">
                <div class="div-link1"><img id='img_16' src='<?php echo Yii::app()->request->baseUrl; ?>/images/index/arrows1.png' border='0'  /> Statistik Pengunjung</div>  
                <div class="div-link2">
                <img id='img_16' src='<?php echo Yii::app()->request->baseUrl; ?>/images/counter/images/online.png' border='0'  /><span style="font-size:12px;color:#000000">Online : <?php echo $pengunjungonline; ?> </span><br />
                <img id='img_16' src='<?php echo Yii::app()->request->baseUrl; ?>/images/counter/images/online.png' border='0'  /><span style="font-size:12px;color:#000000">Hari Ini : <?php echo $pengunjung; ?> </span><br />
                <img id='img_16' src='<?php echo Yii::app()->request->baseUrl; ?>/images/counter/images/online.png' border='0'  /><span style="font-size:12px;color:#000000">Total : <?php echo $totalpengunjung; ?> </span><br />
                
                
  </div>

